package com.example.springdemo.service;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppMergePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.example.springdemo.config.AlipayConfig1;
import com.example.springdemo.pojo.AlipayBean;
import org.springframework.stereotype.Service;

@Service
public class AlipayServiceImpl implements AlipayService{


    @Override
    public String aliPay(AlipayBean alipayBean) throws AlipayApiException {
       //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                AlipayConfig1.gatewayUrl,
                AlipayConfig1.appId,
                AlipayConfig1.privateKey,
                "json",
                AlipayConfig1.charset,
                AlipayConfig1.publicKey,
                AlipayConfig1.signType
        );
        //设置请求参数
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();  //发出支付请求
        alipayRequest.setNotifyUrl(AlipayConfig1.notify_url);
        alipayRequest.setReturnUrl(AlipayConfig1.returnUrl);

        System.out.println(JSON.toJSONString(alipayBean));
        alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println(result+"");
        return result;
    }
}
